package org.tobuy.servlet;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.tobuy.vo.Score;
import org.tobuy.vo.User;
import org.tobuy.factory.ScoreDAOFactory;

public class AddScore extends HttpServlet {


	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path = "rating_pass.jsp";
		String info = new String();
		User user = (User)req.getSession().getAttribute("user");
		int score_value= Integer.parseInt((String) req.getParameter("score_value"));
		String score_type = req.getParameter("score_type");
		String target_id = req.getParameter("target_id");
		if(user==null){
			info ="Please login first in order to rate!";
		}else{
			if(user!=null&&target_id.equals(user.getUser_id())){
				info = "Please notice that you can not rate yourself!";
			}else{
				Score score = new Score();
				score.setScore_value(score_value);
				score.setTarget_id(target_id);
				score.setTarget_type(score_type);
				try {
					if(ScoreDAOFactory.getIScoreDAOInstance().addScore(score)){
						info = "Rating successful!";
					}else{
						info = "Rating failed! Please try again later!";
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		req.setAttribute("info", info);
		req.getRequestDispatcher(path).forward(req, resp);
	
	}

	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req,resp);

	
	}

}
